package controller;
import enums.Direction;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PlayerController extends Controller implements KeyListener {

	private boolean up;
	private boolean left;
	private boolean down;
	private boolean right;
	private int directionCode;

	public PlayerController() {
		this.up = this.left = this.down = this.right = false;
		this.directionCode = 0;
	}

	public Direction getDesiredDirection() {
		this.directionCode = 0;
		if (up) directionCode++;
		if (down) directionCode += 2;
		if (left) directionCode += 4;
		if (right) directionCode += 8;
		
		switch (directionCode) {
		case 0:
		case 3:
		case 12:
		case 15:
			return Direction.STILL;
		case 1:
		case 13:
			return Direction.N;
		case 2:
		case 14:
			return Direction.S;
		case 4:
		case 7:
			return Direction.W;
		case 8:
		case 11:
			return Direction.E;
		case 5:
			return Direction.NW;
		case 6:
			return Direction.SW;
		case 10:
			return Direction.SE;
		case 9:
			return Direction.NE;
		default:
			return Direction.STILL;
		}
	}

	public void keyPressed(KeyEvent ke) {
		switch (ke.getKeyChar()) {
		case 'w':
			up = true;
			break;
		case 'a':
			left = true;
			break;
		case 's':
			down = true;
			break;
		case 'd':
			right = true;
			break;
		}
	}

	public void keyReleased(KeyEvent ke) {
		switch (ke.getKeyChar()) {
		case 'w':
			up = false;
			break;
		case 'a':
			left = false;
			break;
		case 's':
			down = false;
			break;
		case 'd':
			right = false;
			break;
		}
	}

	public void keyTyped(KeyEvent ke) {
	}

}
